package cn.iocoder.yudao.module.promotion.controller.admin.paymentterm;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.promotion.controller.admin.paymentterm.vo.*;
import cn.iocoder.yudao.module.promotion.dal.dataobject.paymentterm.PaymentTermDO;
import cn.iocoder.yudao.module.promotion.service.paymentterm.PaymentTermService;

@Tag(name = "管理后台 - 收款方式")
@RestController
@RequestMapping("/promotion/payment-term")
@Validated
public class PaymentTermController {

    @Resource
    private PaymentTermService paymentTermService;

    @PostMapping("/create")
    @Operation(summary = "创建收款方式")
    @PreAuthorize("@ss.hasPermission('promotion:payment-term:create')")
    public CommonResult<Long> createPaymentTerm(@Valid @RequestBody PaymentTermSaveReqVO createReqVO) {
        return success(paymentTermService.createPaymentTerm(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新收款方式")
    @PreAuthorize("@ss.hasPermission('promotion:payment-term:update')")
    public CommonResult<Boolean> updatePaymentTerm(@Valid @RequestBody PaymentTermSaveReqVO updateReqVO) {
        paymentTermService.updatePaymentTerm(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除收款方式")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('promotion:payment-term:delete')")
    public CommonResult<Boolean> deletePaymentTerm(@RequestParam("id") Long id) {
        paymentTermService.deletePaymentTerm(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得收款方式")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('promotion:payment-term:query')")
    public CommonResult<PaymentTermRespVO> getPaymentTerm(@RequestParam("id") Long id) {
        PaymentTermDO paymentTerm = paymentTermService.getPaymentTerm(id);
        return success(BeanUtils.toBean(paymentTerm, PaymentTermRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得收款方式分页")
    @PreAuthorize("@ss.hasPermission('promotion:payment-term:query')")
    public CommonResult<PageResult<PaymentTermRespVO>> getPaymentTermPage(@Valid PaymentTermPageReqVO pageReqVO) {
        PageResult<PaymentTermDO> pageResult = paymentTermService.getPaymentTermPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PaymentTermRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出收款方式 Excel")
    @PreAuthorize("@ss.hasPermission('promotion:payment-term:export')")
    @OperateLog(type = EXPORT)
    public void exportPaymentTermExcel(@Valid PaymentTermPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PaymentTermDO> list = paymentTermService.getPaymentTermPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "收款方式.xls", "数据", PaymentTermRespVO.class,
                        BeanUtils.toBean(list, PaymentTermRespVO.class));
    }

}